package com.example.clienteclinicav2;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import com.google.gson.JsonObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ModificarPacienteActivity extends Activity{

	private EditText nombreEditText;
	private EditText direccionEditText;
	private TextView rutTextView;
	private String NAMESPACE = "http://capaservicio";
	private String URL = "http://192.168.0.12:8080/ServicioPrestacionesClinica/services/PacienteORM";
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modificar_paciente);
        nombreEditText = (EditText) findViewById(R.id.txtNombre);
        rutTextView = (TextView) findViewById(R.id.txtRut);
        direccionEditText = (EditText) findViewById(R.id.txtDireccion);
       // new cargarPeliculaBackground.execute(jsonObjectPelicula.toString());
        
        Bundle bundle=getIntent().getExtras();
        nombreEditText.setText(bundle.getString("nombre"));
        direccionEditText.setText(bundle.getString("direccion"));
        rutTextView.setText(bundle.getString("rut"));
    	Log.i("registro del paciente:", bundle.getString("nombre")+bundle.getString("direccion")+bundle.getString("rut"));
    }
	
	public void ingresarModificacionPaciente(View v)
	{
		JsonObject jsonObjectPelicula = new JsonObject();
		String[] rut = rutTextView.getText().toString().split("-");
		jsonObjectPelicula.addProperty("cuerpo",rut[0]);
		jsonObjectPelicula.addProperty("identifica", rut[1]);
		jsonObjectPelicula.addProperty("nombre", nombreEditText.getText().toString());
		jsonObjectPelicula.addProperty("direccion", direccionEditText.getText().toString());
		
		new editarPacienteBackground().execute(jsonObjectPelicula.toString());
	}
	
class editarPacienteBackground extends AsyncTask<String, Void, String>{
		
		private ProgressDialog progressDialog;

		@Override
		protected void onPreExecute() {
    		progressDialog = ProgressDialog.show(
    				ModificarPacienteActivity.this,
    				"Prescli V1.0", 
    				"Espere mientras se cargan los pacientes...", 
    				true);
			super.onPreExecute();
		}
		
		@Override
		protected String doInBackground(String... params) {

			String resultado = new String();
			String METHOD_NAME = "updatePaciente";
			String SOAP_ACTION = NAMESPACE +"/" +METHOD_NAME;
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			soapEnvelope.dotNet = true;
			soapEnvelope.implicitTypes = true;
			soapEnvelope.setOutputSoapObject(request);

			request.addProperty("contPaciente", params[0]);

			HttpTransportSE aht = new HttpTransportSE(URL);

			try {
				aht.call(SOAP_ACTION, soapEnvelope);
				SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
				resultado = resultString.toString();

			} catch (IOException e) {
				e.printStackTrace();
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			}

			return resultado;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			progressDialog.dismiss();
			//Valido que tenga la informacion necesaria
			if("1".equals(result)){
				Toast.makeText(ModificarPacienteActivity.this, "Editado correctamente", Toast.LENGTH_LONG).show();
				finish();
			}else{
				Toast.makeText(ModificarPacienteActivity.this, "Error al obtener la informacion necesaria", Toast.LENGTH_LONG).show();
			}
		}
	}
}
